//
//  XS_LearnTimeView.swift
//  XueDao
//
//  Created by Mac on 2020/12/14.
//

import UIKit
import AAInfographics

class XS_LearnTimeView: UIView {
    
    let titleArray = ["周学习时长","周学习课程","连续学习"]
    
    var _model : XS_LearnTimeModel!
    public var model : XS_LearnTimeModel!{
        set{
            _model = newValue
            //设置数据
            let weekTimeBtn = bgView.viewWithTag(100) as! UIButton
            
            let titleAttDict: [NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: .scale*12),
                                                                .foregroundColor : UIColor.qmui_color(withHexString: "#AAB1B7")!]
            let strAttDict : [NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: .scale*20, weight: .medium),
                                                               .foregroundColor : UIColor.qmui_color(withHexString: "#383C3F")!]
            let suffixAttDict : [NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: .scale*10, weight: .medium)]
            let weekTimeSuffixAttDict : [NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: .scale*13, weight: .medium)]
            let hour = newValue.weekLearnTime / 60
            let min = newValue.weekLearnTime % 60
            let hourTimeStr = "h"
            let minTimeStr = "min"
            let hourStr = hour > 0 ? String.init(format: "%ld%@ ", hour, hourTimeStr) : ""
            let weekTimeStr = String.init(format: "\n%@%ld%@", hourStr, min, minTimeStr)
            let weekTimeAllStr = String.init(format: "%@%@", titleArray[0], weekTimeStr)
            let weekTimeAttr = NSMutableAttributedString.init(string: weekTimeAllStr)
            weekTimeAttr.addAttributes(titleAttDict, range: NSRange.init(location: 0, length: titleArray[0].count))
            weekTimeAttr.addAttributes(strAttDict, range: NSRange.init(location: titleArray[0].count, length: weekTimeStr.count))
            
            if weekTimeAllStr.contains(hourTimeStr) {
                let hourStrRange: Range = weekTimeAllStr.range(of: hourTimeStr)!
                let hourStrLocation = weekTimeAllStr.distance(from: hourTimeStr.startIndex, to: hourStrRange.lowerBound)
                weekTimeAttr.addAttributes(weekTimeSuffixAttDict, range: NSRange.init(location: hourStrLocation, length: hourTimeStr.count))
            }
            let minStrRange: Range = weekTimeAllStr.range(of: minTimeStr)!
            let minStrLocation = weekTimeAllStr.distance(from: minTimeStr.startIndex, to: minStrRange.lowerBound)
            weekTimeAttr.addAttributes(weekTimeSuffixAttDict, range: NSRange.init(location: minStrLocation, length: minTimeStr.count))
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = .scale*3
            paragraphStyle.alignment = .center
            weekTimeAttr.addAttributes([.paragraphStyle : paragraphStyle],
                                       range: NSRange.init(location: 0, length: titleArray[0].count + weekTimeStr.count))
            weekTimeBtn.setAttributedTitle(weekTimeAttr, for: .normal)
            
            let weekNumBtn = bgView.viewWithTag(101) as! UIButton
            let weekNumClassStr = "节"
            let weekNumStr = String.init(format: "\n%ld%@", newValue.weekLearnNum, weekNumClassStr)
            let weekNumAttr = NSMutableAttributedString.init(string: titleArray[1] + weekNumStr)
            weekNumAttr.addAttributes(titleAttDict, range: NSRange.init(location: 0, length: titleArray[1].count))
            weekNumAttr.addAttributes(strAttDict, range: NSRange.init(location: titleArray[1].count, length: weekNumStr.count))
            weekNumAttr.addAttributes(suffixAttDict, range: NSRange.init(location: titleArray[1].count + weekNumStr.count - weekNumClassStr.count, length: weekNumClassStr.count))
            weekNumAttr.addAttributes([.paragraphStyle : paragraphStyle],
                                      range: NSRange.init(location: 0, length: titleArray[1].count + weekNumStr.count))
            weekNumBtn.setAttributedTitle(weekNumAttr, for: .normal)
            
            let continueTimeBtn = bgView.viewWithTag(102) as! UIButton
            let continueTimeDayStr = "天"
            let continueTimeStr = String.init(format: "\n%ld%@", newValue.conLearnDays, continueTimeDayStr)
            let continueTimeAttr = NSMutableAttributedString.init(string: titleArray[2] + continueTimeStr)
            continueTimeAttr.addAttributes(titleAttDict, range: NSRange.init(location: 0, length: titleArray[2].count))
            continueTimeAttr.addAttributes(strAttDict, range: NSRange.init(location: titleArray[2].count, length: continueTimeStr.count))
            continueTimeAttr.addAttributes(suffixAttDict, range: NSRange.init(location: titleArray[2].count + continueTimeStr.count - continueTimeDayStr.count, length: continueTimeDayStr.count))
            continueTimeAttr.addAttributes([.paragraphStyle : paragraphStyle],
                                           range: NSRange.init(location: 0, length: titleArray[2].count + continueTimeStr.count))
            continueTimeBtn.setAttributedTitle(continueTimeAttr, for: .normal)
            
            let chartModel = AAChartModel()
                        .chartType(.areaspline)//图表类型
                        .title("")//图表主标题
                        .subtitle("")//图表副标题
                        .inverted(false)//是否翻转图形
                        .yAxisTitle("")// Y 轴标题
                        .legendEnabled(false)//是否启用图表的图例(图表底部的可点击的小圆点)
                        .tooltipValueSuffix("")//浮动提示框单位后缀
                        .categories(["周一", "周二", "周三", "周四", "周五", "周六",
                                     "周日"])
                        .colorsTheme(["#2ABD9C"])//主题颜色数组
                        .markerRadius(0)
                        .series([
                            AASeriesElement()
                                .name("")
                                .data(newValue.weekLearnTimeAnyDay)
                                ])
            aaChartView.aa_drawChartWithChartModel(chartModel)
        }
        get{
            return _model
        }
    }
    
    lazy var titleLabel : UILabel = {
        let titleLabel = UILabel.init(frame: .zero)
        titleLabel.textColor = UIColor.qmui_color(withHexString: "#383C3F")
        titleLabel.font = .systemFont(ofSize: 18, weight: .semibold)
        titleLabel.text = "学习时长"
        return titleLabel
    }()
    
    lazy var bgView : UIView = {
        let bgView = UIView.init(frame: .zero)
        bgView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F7")
        bgView.layer.cornerRadius = 4
        // shadowCode
        bgView.layer.shadowRadius = 12
        bgView.layer.shadowOffset = CGSize(width: 0, height: 2)
        bgView.layer.shadowColor = UIColor(red: 0.18, green: 0.20, blue: 0.20, alpha: 0.06).cgColor
        return bgView
    }()
    
    lazy var aaChartView : AAChartView = {
        let chartViewWidth  = CGFloat.screenWidth - (16 * 2)
        let chartViewHeight = 185
        let aaChartView = AAChartView()
        aaChartView.isUserInteractionEnabled = false
        aaChartView.frame = CGRect(x:0,y:72,width:Int(chartViewWidth),height:chartViewHeight)
        // 设置 aaChartView 的内容高度(content height)
        // aaChartView?.contentHeight = self.view.frame.size.height
        aaChartView.layer.borderWidth = 1
        aaChartView.layer.borderColor = UIColor(red: 0.18, green: 0.20, blue: 0.20, alpha: 0.06).cgColor
        return aaChartView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(8)
        }
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(257)
        }
        let buttonWidth = (Int(CGFloat.screenWidth) - 16 * 2 ) / titleArray.count
        for index in 0...titleArray.count - 1 {
            let button = UIButton.init(frame: CGRect.init(x: buttonWidth * index, y: 0, width: buttonWidth, height: 72))
            button.tag = 100 + index
            button.titleLabel?.numberOfLines = 0
            button.titleLabel?.textAlignment = .center
            bgView.addSubview(button)
        }
        
        bgView.addSubview(aaChartView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
